package {
	/**
	 * QRCode library @author Kazuhiko Arase 
	 * http://www.d-project.com/
	 * http://www.d-project.com/qrcode/index.html
	 * QRCode library @copyright Kazuhiko Arase, not covered by the implementation's MIT license.
	 * 
	 * Implementation by 
	 *		Marcos Weskamp 
	 * 		marcos@marumushi.com 
	 * 		marumushi.com
	 * 
	 * Celisa Library is @copyright marcos@marumushi.com
	 * */
    import celisa.core.Application;
    import celisa.text.InputTextField;
    import celisa.text.TextField;
    import celisa.utils.Blinker;
    
    import com.d_project.qrcode.view.QRCode;
    
    import flash.events.Event;

	[SWF (frameRate="61", backgroundColor="#FFFFFF", width="700",height="700")]
    public class QRCodeType extends Application {

		
		private var input		:TextInput	
		private var qrcode		:QRCode 			= new QRCode(600);		
		private var error		:TextField			= new TextField(0xFF0000);
		
		private var maxDirectInputLength:int		= 30;
		
        public function QRCodeType() {
        	GlobalStyle.init();
        	input = new TextInput();
        	debug();
  			error.text			= "Ouch, string is too long.\nThis QRCode supports a maximum of 213 characters";
 			input.defaultText 	= 'type something over here...';
 			qrcode.text			= 'Hello!';

 			input.setSize(300,30);
  			error.visible =false;
  			error.multiline = true;
  			error.selectable = false;
        	addChild(input);
        	addChild(qrcode);
        	addChild(error);
        	qrcode.text = '';
        	stage.addEventListener(Event.RESIZE,onResize);
			input.addEventListener(InputTextField.INPUT,		onInput);
			input.addEventListener(InputTextField.DELAYEDINPUT,	onDelayedInput);
			onResize();
        }
        
        private function onResize(evt:*=null):void{
        	var ww:int = (stage.stageWidth/2);
        	var hh:int = (stage.stageHeight/2);
            qrcode.x = int(ww-(qrcode.width/2));
        	qrcode.y = int(hh-(qrcode.height/2));
        	input.x = int(ww-(input.background.width/2));
        	input.y = int(qrcode.y+qrcode.height);
        	error.x = input.x;
        	error.y = hh;//input.bottom+10;
        }
        
        private function update():void{
        	error.visible = false;
        	try{
        		qrcode.text = input.text;
        	}catch(any:*){
        		error.visible=true;
        		Blinker.blink(error);
        	}
        }
        private function onInput(evt:*=null):void{
        	if(input.text.length <= maxDirectInputLength)
        		update();
        }
        
        private function onDelayedInput(evt:*=null):void{
        	if(input.text.length > maxDirectInputLength)
        		update();
        }
    }
}



	
import celisa.styles.StyleFormat;

import flash.text.AntiAliasType;

import celisa.ui.TextInput;

class GlobalStyle {
	
	private static var style:StyleFormat;
	
	public static function init():void{

			var styleFormat:StyleFormat = StyleFormat.getDefault();

			styleFormat.textFormat.size 	= 18;
			styleFormat.embedFonts			= false;
			styleFormat.textAntiAliasType 	= AntiAliasType.ADVANCED;
			styleFormat.textFormat.font		= 'Arial';
			styleFormat.foreground.out 		= 0xF3F3F3;
			styleFormat.foreground.selected = 0x898989;
			styleFormat.foreground.down		= 0xE0E0E0;
			styleFormat.foreground.over		= 0x232323;
			styleFormat.background.out 		= 0xCCCCCC;
			styleFormat.background.over		= 0xC0C0C0;
			styleFormat.toolTipColor 		= 0xBBBBBB;
			styleFormat.text.out 			= 0x404040;
			styleFormat.margin 				= 3;
			styleFormat.focusGlow.alpha		= 0.7;
			styleFormat.focusGlow.color		= 0x99FF00;
			
			style = styleFormat;
			
	}
}

class TextInput extends celisa.ui.TextInput {
	
	[Embed (source="../embed/search_textinput.png")]
	private static const backgroundImage:Class

	function TextInput(){
		background = new backgroundImage();
		textField.height=30;
		submitOnBackSpace = true;
	}
	
}


	